package com.ts.api.module.match.plan.internal.distribute.strategy;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.id.IdUtils;
import com.ts.api.common.constant.api.ApiVersionModeEnum;
import com.ts.api.common.constant.product.ProductPlanVersionMode;
import com.ts.api.module.api.convert.core.http.client.ApiHttpMessage;
import com.ts.api.module.api.convert.org.handler.ApiLocalOrgConvertHandler;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.channel.entity.qo.ChannelUserQO;
import com.ts.api.module.channel.service.ChannelUserService;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistribute;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMd5Field;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePost;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeUserRequest;
import com.ts.api.module.match.plan.model.AbstractPlanMatch;
import com.ts.api.module.match.plan.model.ApiPlanMatch;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  本地机构分发
 *
 *  @author Pan
 */
@Service
public class LocalOrgDistribute implements PlanDistributeStrategy<ApiLocalOrgConvertHandler> {

    @Autowired
    private BaseDistribute baseDistribute;
    @Autowired
    private ChannelUserService channelUserService;

    @Override
    public PlanDistributeResult preValidate(PlanDistribute planDistribute) {
        //  基础校验
        PlanDistributeResult planDistributeResult = baseDistribute.preValidate(planDistribute);
        if (!planDistributeResult.getDistributeStatus()) {
            return planDistributeResult;
        }

        ProductPlanPO productPlanPO = planDistribute.getProductPlanPO();

        //  只有md5模式才检测排重包
        if (ProductPlanVersionMode.MD5.hasVersionMode(productPlanPO)) {
            if (!baseDistribute.validExistMd5Filter(planDistribute)) {
                return PlanDistributeResult.ofFail(planDistribute, "排重包中已发现该用户");
            }
        }
        return planDistributeResult;
    }

    @Override
    public PlanDistributeResult distribute(PlanDistribute planDistribute) {
        ProductApplyFlowPO currentDistributeFlow = planDistribute.getCurrentDistributeFlow();
        currentDistributeFlow.setReqData("本地甲方CRM");
        return PlanDistributeResult.ofEmptySuc(planDistribute);
    }

    @Override
    public PlanDistributeField parseResponse(PlanDistribute planDistribute, ApiHttpMessage apiHttpMessage, ApiLocalOrgConvertHandler handler) throws Exception {
        PlanDistributeUserRequest planDistributeUserRequest = planDistribute.getPlanDistributeUserRequest();
        ProductPO productPO = planDistribute.getProductPO();
        ProductPlanPO productPlanPO = planDistribute.getProductPlanPO();
        String reqId = planDistribute.getReqId();

        //  API渠道掩码分发到本地crm机构处理
        AbstractPlanMatch abstractPlanMatch = planDistribute.getAbstractPlanMatch();
        if (abstractPlanMatch instanceof ApiPlanMatch apiPlanMatch) {
            ApiKeyPO apiKeyPO = apiPlanMatch.getApiKeyPO();

            //  掩码分发本地机构处理
            if (Eq.objectOr(apiKeyPO.getApiVersionMode(),
                    ApiVersionModeEnum.PHONE_MASK_8.getVersionMode(),
                    ApiVersionModeEnum.PHONE_MASK_9.getVersionMode(),
                    ApiVersionModeEnum.PHONE_MASK_10.getVersionMode())) {

                String phoneMask = planDistributeUserRequest.getPhoneMask();

                //  本地查询结果返回
                ChannelUserQO channelUserQO = ChannelUserQO.createPhone(phoneMask);
                List<ChannelUserRegisterPO> list = channelUserService.listRightMatchPhone(channelUserQO);
                List<String> listPhoneMd5 = CollUtils.toList(list, user -> user.getPhoneMd5());
                //  掩码模式返回
                return PlanDistributeField.ofOrgPhoneMask(productPO, productPlanPO, listPhoneMd5, reqId, IdUtils.getSimpleUuid());
            }
        }

        //  md5直模式返回
        PlanDistributeField planDistributeField = PlanDistributeField.ofOrgPhoneMd5(productPO, productPlanPO, reqId);

        //  设置phoneMd5
        String phoneMd5 = planDistributeUserRequest.getPhoneMd5();
        PlanDistributePhoneMd5Field phoneMd5Field = planDistributeField.getPhoneMd5Field();
        phoneMd5Field.setPhoneMd5(phoneMd5);
        return planDistributeField;
    }

    @Override
    public PlanDistributeResult post(PlanDistributePost planDistributePost) {
        return PlanDistributeResult.ofSuc(planDistributePost);
    }
}
